from django.contrib.auth.backends import ModelBackend

from .models import User


class UserBackend(ModelBackend):
    """ Custom User Authentication """

    def authenticate(self, request, username=None, password=None):
        """ Rewrite authentication method """
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            return None
        if not user.is_active:
            return user
        if user and user.check_password(password):
            return user
        else:
            return None
